import { Controller } from "egg";

class UserController extends Controller {
  /**
   * 处理登录
   */
  public async login() {
    const {
      ctx,
      ctx: { request }
    } = this;
    try {
      ctx.validate({
        user_name: "string",
        user_password: "string"
      });
    } catch (e) {
      ctx.body = { code: 3, msg: e.errors[0].message };
      return;
    }
    const param = request.body;
    const returnData = await this.service.userService.login(param);
    ctx.body = { code: 1, data: returnData };
  }

  /**
   * 处理注册
   */
  public async register() {
    const {
      ctx,
      ctx: { request }
    } = this;
    try {
      ctx.validate({
        user_name: "string",
        user_password: "string"
      });
    } catch (e) {
      ctx.body = { code: 3, msg: e.errors[0].message };
      return;
    }
    const param = request.body;
    await this.service.userService.register(param);
    ctx.body = { code: 1, msg: "注册成功" };
  }
}

export default UserController;
